package com.example.printer2.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.printer2.R;
import com.example.printer2.adapter.po.FileViewHolder;
import com.example.printer2.adapter.po.RecordViewHolder;
import com.example.printer2.po.MyFile;
import com.example.printer2.po.Record;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RecordAdapter extends RecyclerView.Adapter<RecordViewHolder> {
    List<Record> list;

    Context context;
    public RecordAdapter(List<Record> list,Context context)
    {
        this.context=context;
        this.list=list;
    }

    @Override
    public RecordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecordViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_record, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecordViewHolder holder, int position) {
        Record record=list.get(position);
        holder.file_name.setText(record.myFile.getFileName());
        getDate(record,holder.create_time);
        holder.printer.setText(record.Printer);
        if(record.myFile.getType()==0) {
            holder.record_avatar.setImageResource(R.mipmap.word);
        }
        else holder.record_avatar.setImageResource(R.mipmap.pdf);
        View.OnClickListener onClickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId())
                {
                    case R.id.download:
                    {
                        Toast.makeText(context,"下载中",Toast.LENGTH_LONG).show();
                        break;
                    }
                }
            }
        };
        holder.download.setOnClickListener(onClickListener);
    }

    @SuppressLint("ResourceAsColor")
    public void getDate(Record m, TextView textView)
    {
        if(m.myFile.getFile()==null) {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            String d = sdf1.format(m.date);
            textView.setText(d);
        }
        else
        {
            Date adate = new Date(m.date.getTime()+300000);
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String d = "预计"+sdf1.format(adate)+"完成";
            textView.setText(d);
            int color = Color.rgb(220,20,60);
            textView.setTextColor(color);
        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }
}